<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Share extends Controller
{
    /**技术分享
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //技术分享
        $love = Db::name('article')->limit(4)->order('artlove desc')->select();//热门文章

        $links = Db::name('links')->select();//友情链接

        $session = Session::get('user_info');
        $column = Db::name('column')->where('colname','=','技术分享')->find();
        $list = Db::name('article')->where('column_id',$column['column_id'])->select();
        // var_dump($list);
        $roles = array();
        foreach ($list as $val)
        {
            $num = Db::name('artcomment')->where('art_id',$val['art_id'])->count();
            // var_dump($num);
            // var_dump($user['name']);
            $val['num'] = $num;
            $roles[] = $val;

        }

        //登录处理
        $info = Session::get('user_info');

        $user_id = $info['user_id'];
        $art = Db::name('artcomment')->where('user_id', $user_id)->select();
        $posts = Db::name('post')->where('user_id', $user_id)->select();

        return view('share/index',[
            'session' => $session,
            'lists' => $roles,
            'love'=>$love,
            'links'=>$links,

            'user_id' => $info['user_id'],//登录处理
            'username' => $info['username'],
            'sex' => $info['sex'],
            'age' => $info['age'],
            'userpic' => $info['userpic'],
            'tel' => $info['tel'],
            'email' => $info['email'],
            'integral' => $info['bp'],
            'list' => $art,
            'posts' => $posts
        ]);
    }

    public function detail($id)
    {
        //登录处理
        $info = Session::get('user_info');

        $user_id = $info['user_id'];
        $art = Db::name('artcomment')->where('user_id', $user_id)->select();
        $posts = Db::name('post')->where('user_id', $user_id)->select();

        //技术分享详情页
        $session = Session::get('user_info');
        $article = Db::name('article')->where('art_id',$id)->find();
        $column = Db::name('column')->where('column_id',$article['column_id'])->find();
        $count = Db::name('artcomment')->where('art_id',$id)->count();
        $artcom = Db::name('artcomment')->where('art_id',$id)->select();

        $artlist = array();
        //文章评论表
        foreach($artcom as $val)
        {
            $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
            $runcomment = Db::name('runcomment')->where('artcom_id',$val['artcom_id'])->select();

            $val['username'] = $user['username'];
            $val['userpic'] = $user['userpic'];
            $rundata = array();
            //文章追评表
            foreach($runcomment as $v){
                $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
                $v['runcomuser'] = $user['username'];
                $rundata[] = $v;
            }
            $val['runcom'] = $rundata;//把追评信息放入$artcom数组中
            $artlist[] =$val;
        }

        //浏览加一
        $data = Db::name('article')->find($id);
        $views = $data['views'];
        $views = $views + 1;
        $postdata = [
            'views' => $views
        ];
        $links = Db::name('links')->select();//友情链接
        $love = Db::name('article')->limit(5)->order('artlove desc')->select();//热门文章
        Db::name('article')->where('art_id',$id)->update($postdata);
        $list = Db::name('article')->find($id);

        return view('Share/detail',[
            'article'=>$article,//文章
            'session' => $session,//用户缓存
            'column'=>$column,//栏目
            'count'=>$count,//评论数
            'artcom'=>$artlist,//评论内容
            'lists'=>$list,//文章数据
            'user_id' => $info['user_id'],//登录处理
            'love'=>$love,
            'username' => $info['username'],
            'sex' => $info['sex'],
            'age' => $info['age'],
            'userpic' => $info['userpic'],
            'tel' => $info['tel'],
            'email' => $info['email'],
            'integral' => $info['bp'],
            'list' => $art,
            'links'=>$links,
            'posts' => $posts
        ]);
    }

    //文章点赞的ajax传值
    function artlove()
    {

        $data = Db::name('article')->find($_POST['artid']);;
        
        $love = $data['artlove'] + 1 ;
        $artdata = [
            'artlove' => $love
        ];
        // var_dump($artdata);
        $result = Db::name('article')->where('art_id',$_POST['artid'])->update($artdata);
        if($result){
            $info['status'] = true;
            $info['love'] = $love;

        }else{
            $info['status'] = false;
        }
        return $info;


    }
}
